<template>
  <div>
    <cus-allcard style="padding-bottom:0"
                 :showTitle="false">
      <div class="topBody">
        <div class="topBodyItem">
          <span class="itemSpan">账簿年度</span>
          <Select v-model="accountbookId"
                  placeholder="请选择账簿年度"
                  @on-change="accountbookIdChange()"
                  class="itemImport">
            <Option :value="0+''">初始化数据</Option>
            <Option v-for="item in yearList"
                    :value="item.accountbookId"
                    :key="item.year">{{ item.year }}</Option>
          </Select>
        </div>
      </div>
    </cus-allcard>
    <cus-allcard :showTitle="false">
      <div style="margin-top:20px;padding-top:24px">
        <cusTable border
               :loading="form.loading"
               :columns="columns"
               :data="dataList"

               :size="vuex_config.tableSize">
          <template slot-scope="{ row }"
                    slot="abstracttype">
            {{row.abstracttype==0?'经费账':'粮秣账'}}
          </template>
          <template slot-scope="{ row }"
                    slot="kemu">
            {{row.kemu}}
          </template>
          <template slot-scope="{ row }"
                    slot="operation">
            <div class="flex">
              <cus-button v-if="row.shifoumoren==-1"
                          type="text"
                          size="small"
                          style="color:#1E6FFF;padding:0 8px"
                          @onClick="mdfDefaultModApi(row)">
                <Icon style="font-size: 15px;"
                      type="ios-build" />
                设为默认参数
              </cus-button>
              <div v-if="row.shifoumoren!=-1"
                   style="display: flex;align-items: center;">
                <Icon style="font-size: 15px;"
                      type="md-checkmark-circle-outline" />
                默认参数
              </div>
            </div>
          </template>
        </cusTable>
      </div>
    </cus-allcard>
  </div>
</template>
<script>
import {
  getAccountBookByUserId,
  queryAllSmartAccountByAccountBookId,
  mdfDefaultMod
} from '@/api/businessAccountingApi/essentialInformationApi'
export default {
  data () {
    return {
      form: {
        loading: false,
        name: '',
        startTime: '',
        endTime: ''
      },
      // 表头
      columns: [
        {
          title: '序号',
          type: 'index',
          align: 'center',
          minWidth: 60,
        },
        {
          title: '场景',
          key: 'defaultsceneCL',
          align: 'center',
          minWidth: 70,
        },
        {
          title: '摘要名称',
          key: 'abstractname',
          align: 'center',
          minWidth: 200,
        },
        {
          title: '摘要类型',
          slot: 'abstracttype',
          align: 'center',
          minWidth: 70,
        },
        {
          title: '科目',
          key: 'subject',
          align: 'center',
          minWidth: 260,
        },
        {
          title: '操作',
          fixed: 'right', // 右侧固定
          slot: 'operation',
          align: 'center',
          minWidth: 120,
        }
      ],
      dataList: [], // 表格列表
      yearList: [], // 账薄年度列表
      accountbookId: null,//账簿id

    }
  },
  mounted () {


    this.getAccountBookByUserIdApi()// 根据用户id查询账簿 接口
  },

  methods: {
    // 根据用户id查询账簿 接口
    getAccountBookByUserIdApi () {
      this.form.loading = true
      return new Promise((resolve) => {
        getAccountBookByUserId({
          userid: this.vuex_user.base.id // 用户id
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            this.yearList = res.basAccountbookList
            this.accountbookId = res.basAccountbookList[0].accountbookId
            this.queryAllSmartAccountByAccountBookIdApi()// 根据账簿ID查询自动记账摘要 接口
            resolve()
          } else {
            this.accountbookId = '0'
            this.queryAllSmartAccountByAccountBookIdApi()// 根据账簿ID查询自动记账摘要 接口
          }
        })
      })
    },
    // 根据账簿ID查询自动记账摘要 接口
    queryAllSmartAccountByAccountBookIdApi () {
      this.form.loading = true
      return new Promise((resolve) => {
        queryAllSmartAccountByAccountBookId({
          accountBookId: this.accountbookId, //账簿id(传0表示查询初始化数据)	
          isZhineng: 1, //智能(固定传1)	
          isvalid: 1, //是否在用(固定传1)
        }).then((res) => {
          this.form.loading = false
          if (res.retCode === 0) {
            this.dataList = res.data
            //将默认场景    智能记账场景:1开头表示入库, 2开头表示出库,3开头表示盘点  从01开始,比如101,201,301 
            for (let s = 0; s < this.dataList.length; s++) {
              this.dataList[s].shifoumoren = this.dataList[s].defaultscene.indexOf("-M")
              if (this.dataList[s].defaultscene.substring(0, 1) == 1) {
                this.dataList[s].defaultsceneCL = "入库"
              }
              if (this.dataList[s].defaultscene.substring(0, 1) == 2) {
                this.dataList[s].defaultsceneCL = "出库"
              }
              if (this.dataList[s].defaultscene.substring(0, 1) == 3) {
                this.dataList[s].defaultsceneCL = "盘盈"
              }
              if (this.dataList[s].defaultscene.substring(0, 1) == 4) {
                this.dataList[s].defaultsceneCL = "交伙"
              }
              if (this.dataList[s].defaultscene.substring(0, 1) == 5) {
                this.dataList[s].defaultsceneCL = "退伙"
              }
              if (this.dataList[s].defaultscene.substring(0, 1) == 6) {
                this.dataList[s].defaultsceneCL = "盘亏"
              }
            }
            resolve()
          } else {
            this.$Message.error(res.retMsg)
          }
        })
      })
    },
    // 设置默认参数 接口
    mdfDefaultModApi (valueData) {
      this.form.loading = true
      return new Promise((resolve) => {
        mdfDefaultMod({
          abstractId: valueData.abstractId,
          abstractcode: valueData.abstractcode,
          abstractname: valueData.abstractname,
          abstractType: valueData.abstracttype,
          defaultscene: valueData.defaultscene,
          isvalid: 1,
          isZhineng: 1
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            this.queryAllSmartAccountByAccountBookIdApi()// 根据账簿ID查询自动记账摘要 接口
            resolve()
          } else {
            this.$Message.error(res.resultmsg)
          }
        })
      })
    },
    // 账簿年度改变回调
    accountbookIdChange () {
      this.queryAllSmartAccountByAccountBookIdApi()// 根据账簿ID查询自动记账摘要 接口
    },
  }
}
</script>
<style lang="less" scoped>
.topBody {
  display: flex;
  padding-top: 24px;
  align-items: center;
  flex-wrap: wrap;
  .topBodyItem {
    display: flex;
    align-items: center;
    margin-right: 12px;
    margin-bottom: 20px;
    .itemSpan {
      white-space: nowrap;
      width: 80px;
      text-align: right;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.85) !important;
      margin-right: 10px;
    }
    .itemImport {
      width: 200px;
    }
  }
}
</style>
